import requests
from datetime import datetime
#From tianqi AI at GitCode.

def get_latest_commit_id(repo_owner, repo_name, branch="master"):
    """
    获取 GitCode 仓库最新 commit ID
    
    Args:
        repo_owner: 仓库所有者（用户名或组织名）
        repo_name:  仓库名称
        branch:     分支名（默认master）
    
    Returns:
        commit_id: 最新的 commit SHA 值
        或错误信息字符串
    """
    # 构建 GitCode API 地址 (GitCode 使用 GitLab API 协议)
    api_url = f"https://gitcode.net/api/v4/projects/{repo_owner}%2F{repo_name}/repository/branches/{branch}"
    
    try:
        response = requests.get(api_url, timeout=10)
        response.raise_for_status()  # 检查HTTP错误
        
        data = response.json()
        return data['commit']['id']
        
    except requests.exceptions.RequestException as e:
        return f"请求失败：{str(e)}"
    except (KeyError, ValueError):
        return "解析响应数据失败"

# 获取当前日期和时间
current_time = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
print(f"当前时间: {current_time}")

# 示例：查询 TianQi AI 仓库最新 commit
owner = "openharmony"  # 替换为实际所有者
repo = "manifest"  # 替换为实际仓库名
branch = "master"     # 替换为目标分支

print(f"\n查询仓库: {owner}/{repo} (分支: {branch})")
commit_id = get_latest_commit_id(owner, repo, branch)

if len(commit_id) == 40 and commit_id.isalnum():
    print(f"最新 commit ID: {commit_id}")
    print(f"Commit URL: https://gitcode.net/{owner}/{repo}/-/commit/{commit_id}")
else:
    print(f"错误: {commit_id}")
    